package com.topdt.jbpm.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.thx.common.util.AppContext;



/**
 * 图片转html类
 * @author fengjinshou
 *
 */
public class ImageToHtml {
	private static final Log logger = LogFactory.getLog(ImageToHtml.class);
	private String classPath = this.getClass().getClassLoader().getResource("").getPath();
	private String context = "";//项目所在服务器上的绝对路径

	public ImageToHtml() {
		classPath = this.getClass().getClassLoader().getResource("").getPath();
		String[] paths = this.classPath.split("/");
		for (int i = 0; i < paths.length; i++) {
			if (i < paths.length - 2) {
				if (!paths[i].equals("")) {
					context += paths[i] + "/";
				}
			}
		}
	}

	/**
	 * 图片转html
	 * @author 冯晋首
	 * @date   2015-3-30 下午6:31:28
	 * @param imageNames
	 * @param htmlName
	 */
	public  void generateHtml(String imageNames,String htmlName){
		String fileUploadPathHtml = "/" + this.context + AppContext.getProperties("fileUploadPathHtml");
		//String uploadPath = AppContext.getProperties("uploadPath");
		String oaStaticAdd = AppContext.getProperties("oaStaticAdd");
		String fileUploadPathImage = AppContext.getProperties("fileUploadPathImage");
		StringBuffer sb = new StringBuffer();
		sb.append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"+"\r\n");
		sb.append("<html>"+"\r\n");
		sb.append("<head>"+"\r\n");
			sb.append("<title>MyHtml.html</title>"+"\r\n");
		    sb.append("<meta http-equiv=\"keywords\" content=\"keyword1,keyword2,keyword3\">"+"\r\n");
		    sb.append("<meta http-equiv=\"description\" content=\"this is my page\">"+"\r\n");
		    sb.append("<meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\">"+"\r\n");
		    sb.append("<!--<link rel=\"stylesheet\" type=\"text/css\" href=\"./styles.css\">-->"+"\r\n");
		    sb.append("</head>"+"\r\n");
		sb.append("<body style=\"wdith:100%;text-align:center;background-color:#000;\">"+"\r\n");
		sb.append("<div style=\"width:100%;heigth:auto;\">"+"\r\n");
		if(StringUtils.isNotBlank(imageNames)){
			if(imageNames.indexOf(",")!=-1){
				String[] imageArray = imageNames.split(",");
				for (int i = 0; i < imageArray.length; i++) {
					String imagePath = fileUploadPathImage+"/"+imageArray[i];
					String imgSrc = oaStaticAdd+"/workflow/download.action?inputPath="+imagePath;
					String img = "<img src=\""+imgSrc+"\"/>";
					sb.append(img+"<br>"+"\r\n");
				}
			}else{
				String imagePath = fileUploadPathImage+"/"+imageNames;
				String imgSrc = oaStaticAdd+"/workflow/download.action?inputPath="+imagePath;
				String img = "<img src=\""+imgSrc+"\"/>";
				sb.append(img+"<br>"+"\r\n");
			}
		}
		sb.append("</div>"+"\r\n");
		sb.append("</body>"+"\r\n");
		sb.append("</html>"+"\r\n");
		try {
			File htmlPath = new File(fileUploadPathHtml);
			if(!htmlPath.exists()){
				htmlPath.mkdirs();
			}
			File file = new File(fileUploadPathHtml+"/"+htmlName);
			PrintStream ps = new PrintStream(new FileOutputStream(file));
			ps.println(sb.toString());
			ps.close();
		} catch (FileNotFoundException e) {
			logger.error("写入html片段时出错"+e);
		}
		logger.debug("成功写入html片段");
	}

	public static void main(String[] args) {
		//new  ImageToHtml().generateHtml("2015033019115346642.jpg,2015033019115348322.jpg", "4343434343.html");
	}
}
